package com.macro.mall.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.PmProdPrptyMapper;
import com.macro.mall.mapper.PmProdPrptyValMapper;
import com.macro.mall.mapper.PmPrptyMapper;
import com.macro.mall.mapper.PmPrptyValMapper;
import com.macro.mall.model.*;
import com.macro.mall.product.constant.Constants;
import com.macro.mall.product.dto.PmPrptyDto;
import com.macro.mall.product.dto.PmPrptyValDto;
import com.macro.mall.product.service.PmProdPrptyService;
import com.macro.mall.product.service.PmPrptyService;
import com.macro.mall.product.util.DozerUtils;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PmProdPrptyServiceImpl implements PmProdPrptyService {
    @Autowired
    private PmProdPrptyMapper pmProdPrptyMapper;
    @Autowired
    private PmProdPrptyValMapper pmProdPrptyValMapper;
    @Autowired
    private Mapper mapper;

//    @Override
//    public int savePmPrpty(PmPrptyDto pmPrptyDto) {
//        PmPrpty pmPrpty= mapper.map(pmPrptyDto,PmPrpty.class);
//        int count =0;
//        if(pmPrpty.getId()!=null && pmPrpty.getId()>0){
//            count= pmPrptyMapper.updateByPrimaryKey(pmPrpty);
//        }else{
//            pmPrpty.setStatus(Constants.STATUS_VALID);
//            count= pmPrptyMapper.insert(pmPrpty);
//            if(StringUtils.isBlank(pmPrpty.getPrptyCode())){
//                PmPrpty pmPrptyTemp = new PmPrpty();
//                pmPrptyTemp.setId(pmPrpty.getId());
//                pmPrptyTemp.setPrptyCode(pmPrpty.getId().toString());
//                pmPrptyMapper.updateByPrimaryKeySelective(pmPrptyTemp);
//            }
//        }
//        log.debug("*********************** count = "+count);
//        if(pmPrptyDto.getValList()!=null && pmPrptyDto.getValList().size()>0){
//            PmPrptyVal pmPrptyVal = null;
//            int seq=0;
//            for(PmPrptyValDto valDto : pmPrptyDto.getValList()){
//                pmPrptyVal =  mapper.map(valDto,PmPrptyVal.class);
//                seq++;
//                pmPrptyVal.setPrptySeq(seq);
//                if(pmPrptyVal.getId() !=null && pmPrptyVal.getId()>0 ){
//                    pmPrptyValMapper.updateByPrimaryKey(pmPrptyVal);
//                }else{
//                    pmPrptyVal.setPrptyId(pmPrpty.getId());
//                    pmPrptyVal.setPrptyCode(pmPrpty.getPrptyCode());
//                    pmPrptyVal.setStatus(Constants.STATUS_VALID);
//                    pmPrptyValMapper.insert(pmPrptyVal);
//                }
//            }
//        }
//        return count;
//    }

    @Override
    public List<PmProdPrpty> listProdPrpty(PmProdPrpty param) {
        PmProdPrptyExample example  = new PmProdPrptyExample();
        PmProdPrptyExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.STATUS_VALID);
        if(param.getProdId()!=null){
            criteria.andProdIdEqualTo(param.getProdId());
        }
        example.setOrderByClause(" prpty_seq asc ");
        List<PmProdPrpty> prodPrptyList = pmProdPrptyMapper.selectByExample(example);
        if(prodPrptyList!=null && prodPrptyList.size()>0) {
            for(PmProdPrpty pmProdPrpty:prodPrptyList) {
                PmProdPrptyValExample valExample;
                PmProdPrptyValExample.Criteria valCriteria;
                valExample = new PmProdPrptyValExample();
                valCriteria = valExample.createCriteria();
                valCriteria.andProdPrptyIdEqualTo(pmProdPrpty.getId());
                valCriteria.andStatusEqualTo(Constants.STATUS_VALID);
                valExample.setOrderByClause(" prpty_seq asc ");
                List<PmProdPrptyVal> prptyValList = pmProdPrptyValMapper.selectByExample(valExample);
                pmProdPrpty.setProdPrptyValList(prptyValList);
            }
        }
        return prodPrptyList;
    }

    @Override
    public PmProdPrpty getById(Long id) {
        PmProdPrpty pmProdPrpty = pmProdPrptyMapper.selectByPrimaryKey(id);
        if(pmProdPrpty!=null && pmProdPrpty.getId()!=null){
            PmProdPrptyValExample valExample  ;
            PmProdPrptyValExample.Criteria valCriteria ;
            valExample  = new PmProdPrptyValExample();
            valCriteria = valExample.createCriteria();
            valCriteria.andProdPrptyIdEqualTo(pmProdPrpty.getId());
            valCriteria.andStatusEqualTo(Constants.STATUS_VALID);
            valExample.setOrderByClause(" prpty_seq asc ");
            List<PmProdPrptyVal>  prptyValList = pmProdPrptyValMapper.selectByExample(valExample);
            pmProdPrpty.setProdPrptyValList(prptyValList);
        }
        return pmProdPrpty;
    }
    @Override
    public int deleteProdPrpty(String prodPrptyIds){
        int count =0;
        if(!StringUtils.isBlank(prodPrptyIds)){
            String[] prodPrptyIdArray =prodPrptyIds.split(";");
            for(String prodPrptyId : prodPrptyIdArray){
                int countInfo=pmProdPrptyValMapper.deleteByPrimaryKey(Long.valueOf(prodPrptyId));
                count= count+countInfo;
            }
        }
        return count;
    }

    @Override
    public int deleteProdPrptyVal(String prodPrptyValIds){
        int count =0;
        if(!StringUtils.isBlank(prodPrptyValIds)){
            String[] prptyIValIdArray =prodPrptyValIds.split(";");
            for(String prptyValId : prptyIValIdArray){
                int countInfo=pmProdPrptyValMapper.deleteByPrimaryKey(Long.valueOf(prptyValId));
                count= count+countInfo;
            }
        }
        return count;
    }
}
